import { User as BaseUser } from "entities"
import { LocalService } from "./local-service"
import { errorHandle, PageDataSource } from "maishu-chitu-admin/static";
import { AccountService } from "./account-service";
import { MemberService } from "./member-service";

export type User = BaseUser & { Balance?: number };

let localService = new LocalService(err => errorHandle(err));
let memberService = new MemberService(err => errorHandle(err));

let userDataSource = new PageDataSource<User>({
    select: async (args) => {
        let r = await memberService.members(args);

        let userIds = r.dataItems.map(o => o.Id);
        let accountService = new AccountService(err => errorHandle(err));
        let accounts = await accountService.accountBalances(userIds);

        for (let i = 0; i < r.dataItems.length; i++) {
            let account = accounts.filter(o => o.Id == r.dataItems[i].Id)[0];
            if (account == null)
                continue;

            (r.dataItems[i] as User).Balance = account.Balance;
        }

        return r;
    },
    search: {
        placeholder: "请输入会员手机",
        async execute() {
            return { dataItems: [], totalRowCount: 0 };
        }
    }
})

export let dataSources = {
    user: userDataSource
}